﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using System.Collections.Generic;

namespace RightControl.Service {
    public class ExportFieldsService : BaseService<ExportFields>, IExportFieldsService {
        public List<ExportFields> GetExportFields(string categoryCode, int fdType) {
            return CacheHelper.GetCache<List<ExportFields>>(string.Format("Cache_{0}_{1}", categoryCode, fdType), () => {
                dynamic result = GetListByFilter(new ExportFields() { CategoryCode = categoryCode, FdType = fdType }, new PageInfo(1, 1000));
                return result != null ? result.data as List<ExportFields> : new List<ExportFields>();
            }, 3600);
        }

        public dynamic GetListByFilter(ExportFields filter, PageInfo pageInfo) {
            string _where = GetBaseCondition(filter);
            pageInfo.field = "SortIndex";
            pageInfo.order = "ASC";

            return GetListByFilter(filter, pageInfo, _where);
        }

        public List<ExportFields> GetFieldsForTitle(string categoryCode, int fdType) {
            ExportFields filter = new ExportFields() { CategoryCode = categoryCode, FdType = fdType };
            string _where = GetBaseCondition(filter);
            _where += " and Level <>'' ";
            PageInfo pageInfo = new PageInfo(1, 1000, "Level", "Asc");
            dynamic result = GetListByFilter(filter, pageInfo, _where);
            return result != null ? result.data as List<ExportFields> : new List<ExportFields>();
        }

        public Dictionary<string, string> GetGradeFields(string categoryCode) {
            var fieldsForTitle = GetFieldsForTitle(categoryCode, 1);

            Dictionary<string, string> gradeFields = new Dictionary<string, string>();
            //字段打乱
            foreach (var item in fieldsForTitle) {
                if (!string.IsNullOrEmpty(item.EngFieldNm)) {
                    gradeFields[item.Level] = item.EngFieldNm;
                }
            }
            return gradeFields;
        }

        private static string GetBaseCondition(ExportFields filter) {
            string _where = " where Status=1";
            if (filter.CategoryCode.IsNotEmpty()) {
                _where += string.Format(" and CategoryCode = '{0}'", filter.CategoryCode);
            }
            if (filter.FdType > 0) {
                _where += string.Format(" and FdType = {0}", filter.FdType);
            }
            return _where;
        }

        public bool IsNotExist(ProductDetail filter) {
            string _where = " where 1=1";
            if (filter.OriProductUrl.IsNotEmpty()) {
                _where += string.Format(" and OriProductUrl = '{0}'", filter.OriProductUrl);
            }

            if (filter.UserId > 0) {
                _where += string.Format(" and UserId = {0}", filter.UserId);
            }
            return GetTotal(new SearchFilter() { pageIndex = 1, pageSize = 100, where = _where }) == 0;
        }
    }
}